package de.hska.pwmanager.cryptoGui;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

import de.hska.pwmanager.cryptoGui.controller.CryptHelper;

public class InputBox extends JFrame {
	private JTextField txtPwhash;
	private JTextField txtPathtokey;
	private JTextArea txtrResult;
	public InputBox(String url) {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		txtPwhash = new JTextField();
		txtPwhash.setText("pwHash");
		GridBagConstraints gbc_txtPwhash = new GridBagConstraints();
		gbc_txtPwhash.gridwidth = 2;
		gbc_txtPwhash.insets = new Insets(0, 0, 5, 0);
		gbc_txtPwhash.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPwhash.gridx = 0;
		gbc_txtPwhash.gridy = 0;
		getContentPane().add(txtPwhash, gbc_txtPwhash);
		txtPwhash.setColumns(10);
		
		txtPathtokey = new JTextField();
		txtPathtokey.setText("pathToKey");
		GridBagConstraints gbc_txtPathtokey = new GridBagConstraints();
		gbc_txtPathtokey.insets = new Insets(0, 0, 5, 5);
		gbc_txtPathtokey.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPathtokey.gridx = 0;
		gbc_txtPathtokey.gridy = 1;
		getContentPane().add(txtPathtokey, gbc_txtPathtokey);
		txtPathtokey.setColumns(10);
		
		JButton btnSearch = new JButton("Search...");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openPrivKey();
			}
		});
		GridBagConstraints gbc_btnSearch = new GridBagConstraints();
		gbc_btnSearch.insets = new Insets(0, 0, 5, 0);
		gbc_btnSearch.gridx = 1;
		gbc_btnSearch.gridy = 1;
		getContentPane().add(btnSearch, gbc_btnSearch);
		
		JButton btnDecrypt = new JButton("Decrypt");
		btnDecrypt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				decrypt();
			}
		});
		GridBagConstraints gbc_btnDecrypt = new GridBagConstraints();
		gbc_btnDecrypt.insets = new Insets(0, 0, 5, 0);
		gbc_btnDecrypt.gridx = 1;
		gbc_btnDecrypt.gridy = 2;
		getContentPane().add(btnDecrypt, gbc_btnDecrypt);
		
		txtrResult = new JTextArea();
		txtrResult.setText("result");
		GridBagConstraints gbc_txtrResult = new GridBagConstraints();
		gbc_txtrResult.insets = new Insets(0, 0, 0, 5);
		gbc_txtrResult.fill = GridBagConstraints.BOTH;
		gbc_txtrResult.gridx = 0;
		gbc_txtrResult.gridy = 3;
		getContentPane().add(txtrResult, gbc_txtrResult);
		
		this.txtPwhash.setText(url);
	}
	
	private void openPrivKey() {
	     JFileChooser c = new JFileChooser();
	      // Demonstrate "Save" dialog:
	      int rVal = c.showSaveDialog(this);
	      if (rVal == JFileChooser.APPROVE_OPTION) {
	        this.txtPathtokey.setText(c.getCurrentDirectory().toString() + "/" + c.getSelectedFile().getName());
	      }
	}
	
	public void showMe() {
		setTitle("Manage Types");
		setSize(600, 200);
		super.setVisible(true);
	}
	
	private void decrypt() {
		try {
			String decrypted = CryptHelper.decrypt(this.txtPwhash.getText(), this.txtPathtokey.getText());
			this.txtrResult.setText(decrypted);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
